package com.enjoyor.soa.traffic.rest.efoms.helper;

import java.io.ByteArrayOutputStream;
import java.sql.Connection;
import java.util.Collection;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;

/**
* 用于生成IReport报表生成
* @author zyh
* @version 创建时间：2019-7-17
* @类说明
*/
@SuppressWarnings("deprecation")
public class IReportHelper {
public static byte[] GetReportPdf(String fileName,Map<String,Object> param,Connection conn){
try {
byte[] bytes;
bytes=JasperRunManager.runReportToPdf(fileName,param,conn);
return bytes;
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static byte[] GetReportPdf(String fileName,Map<String,Object> param){
try {
byte[] bytes;
bytes=JasperRunManager.runReportToPdf(fileName, param);
return bytes;
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static byte[] GetReportPdf(String fileName,Map<String,Object> param, Collection list){
try {
byte[] bytes;
JRDataSource jrDataSource = new JRBeanCollectionDataSource(list);
bytes=JasperRunManager.runReportToPdf(fileName, param,jrDataSource);
return bytes;
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static byte[] GetReportRtf(String fileName,Map<String,Object> param, Collection list){
try {
JRDataSource jrDataSource = new JRBeanCollectionDataSource(list);
JasperPrint jasperPrint = JasperFillManager.fillReport(fileName, param, jrDataSource);
JRRtfExporter rtfExporter = new JRRtfExporter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
rtfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
rtfExporter.exportReport();
return out.toByteArray();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

public static byte[] GetReportHtml(String fileName,Map<String,Object> param, Collection list){
try {
JRDataSource jrDataSource = new JRBeanCollectionDataSource(list);
JasperPrint jasperPrint = JasperFillManager.fillReport(fileName, param, jrDataSource);
JRHtmlExporter exporter = new JRHtmlExporter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.exportReport();
return out.toByteArray();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

public static byte[] GetReportHtml(String fileName,Map<String,Object> param, Connection conn){
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(fileName, param,conn);
JRHtmlExporter exporter = new JRHtmlExporter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.exportReport();
return out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static byte[] GetReportExcel(String fileName,Map<String,Object> param, Connection conn){
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(fileName, param,conn);
JRXlsExporter exporter = new JRXlsExporter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.exportReport();
return out.toByteArray();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}

public static byte[] GetReportword(String fileName,Map<String,Object> param, Connection conn){
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(fileName, param,conn);
JRExporter exporter = new JRRtfExporter();
ByteArrayOutputStream out = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.exportReport();
return out.toByteArray();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
